//
//  TTZuanshiRightView.m
//  TaoTuo
//
//  Created by LG on 2020/10/14.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTZuanshiRightView.h"
#import "TTZuanshiScopeView.h"
#import "TTSearchInsertModel.h"
#import "TTSearchModel.h"

@interface TTZuanshiRightView ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) TTZuanshiScopeView *cpView;
@property (nonatomic, strong) TTZuanshiScopeView *colorView;
@property (nonatomic, strong) TTZuanshiScopeView *jdView;
@property (nonatomic, strong) TTZuanshiScopeView *cutView;
@property (nonatomic, strong) TTZuanshiScopeView *czView;
@property (nonatomic, strong) TTZuanshiScopeView *xfView;
@property (nonatomic, strong) TTZuanshiScopeView *xfSubView;
@property (nonatomic, strong) TTZuanshiScopeView *jbView;
@property (nonatomic, strong) TTZuanshiScopeView *fsView;

@property (nonatomic, strong) NSMutableArray *allViewsArray;
/// 当前选中的scopeView
@property (nonatomic, strong) TTZuanshiTextScopeView *selectScopeView;

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *bgImageView;
@property (nonatomic, strong) LLTagView *tagView;

@property (nonatomic, strong) NSArray *xkArray;
@property (nonatomic, strong) NSArray *scArray;
@property (nonatomic, strong) NSArray *jzArray;
@property (nonatomic, strong) NSArray *szArray;
/// 当前选中的
@property (nonatomic, strong) NSArray *selectArray;

@property (nonatomic, assign) BOOL isLeftTF;
/// 记录参数是否发生变化了
@property (nonatomic, copy) NSString *flag;

@end

@implementation TTZuanshiRightView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = UIColor.whiteColor;
        
        [self addObserver:self forKeyPath:@"flag" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
        
        _allViewsArray = [NSMutableArray array];
        _searchNameDic = [NSMutableDictionary dictionary];
        self.searchModel = [[TTSearchModel alloc] init];
        self.searchModel.sourceType = @(1);
//        self.searchModel.categoryName = @"女戒";
        /// 参数发生变化发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"zuanshi_chooseItemsChangedNotify_key" object:[self getParamsDic]];
        
        // 前两个搜索页面的数据
        NSString *path = [[NSBundle mainBundle] pathForResource:@"search_insert" ofType:@"json"];
        
        NSData *data = [NSData dataWithContentsOfFile:path];
        
        NSError *error = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
        // 镶口
        NSMutableArray *xkArray = [NSMutableArray array];
        for (NSDictionary *mDic in dic[@"insertRange"]) {
            [xkArray addObject:[TTSearchInsertModel modelWithJSON:mDic]];
        }
        
        // 手寸
        NSMutableArray *scArray = [NSMutableArray array];
        for (NSDictionary *mDic in dic[@"handInch"]) {
            [scArray addObject:[TTSearchInsertModel modelWithJSON:mDic]];
        }
        
        // 金重
        NSMutableArray *jzArray = [NSMutableArray array];
        for (NSDictionary *mDic in dic[@"goldenWeight"]) {
            [jzArray addObject:[TTSearchInsertModel modelWithJSON:mDic]];
        }
        
        // 手镯内径
        NSMutableArray *szArray = [NSMutableArray array];
        for (NSDictionary *mDic in dic[@"braceletInnerDiameter"]) {
            [szArray addObject:[TTSearchInsertModel modelWithJSON:mDic]];
        }
        
        [self setXkDataArray:xkArray scDataArray:scArray jzDataArray:jzArray szDataArray:szArray];
        
        [self setupSubviews];
        
//        [self setSearchNameDicWithKey:@"0" value:@"女戒"];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden) name:UIKeyboardWillHideNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
        
    }
    return self;
}

- (void)keyboardDidHidden
{
    if (self.xkScopeView.minTF.text.length > 0 && self.xkScopeView.maxTF.text.length > 0) {
        NSString *str = [NSString stringWithFormat:@"%@-%@分", self.xkScopeView.minTF.text, self.xkScopeView.maxTF.text];
        [self setSearchNameDicWithKey:@"5" value:str];
    } else {
        [self setSearchNameDicWithKey:@"5" value:@""];
    }
    if (self.scScopeView.minTF.text.length > 0 && self.scScopeView.maxTF.text.length > 0) {
        NSString *str = [NSString stringWithFormat:@"%@-%@#", self.scScopeView.minTF.text, self.scScopeView.maxTF.text];
        [self setSearchNameDicWithKey:@"6" value:str];
    } else {
        [self setSearchNameDicWithKey:@"6" value:@""];
    }
    if (self.szScopeView.minTF.text.length > 0 && self.szScopeView.maxTF.text.length > 0) {
        NSString *str = [NSString stringWithFormat:@"%@-%@", self.szScopeView.minTF.text, self.szScopeView.maxTF.text];
        [self setSearchNameDicWithKey:@"8" value:str];
    } else {
        [self setSearchNameDicWithKey:@"8" value:@""];
    }
//    if (self.jzScopeView.minTF.text.length > 0 && self.jzScopeView.maxTF.text.length > 0) {
//        NSString *str = [NSString stringWithFormat:@"%@-%@g", self.jzScopeView.minTF.text, self.jzScopeView.maxTF.text];
//        [self setSearchNameDicWithKey:@"7" value:str];
//    } else {
//        [self setSearchNameDicWithKey:@"7" value:@""];
//    }
    
    // [self searchParamsChanged];
}

- (void)keyboardWillHidden
{
    self.bgView.hidden = YES;
}


- (void)keyboardWillAppear:(NSNotification *)notify
{
    NSValue *value = notify.userInfo[UIKeyboardFrameEndUserInfoKey];
    CGFloat keyboardH = value.CGRectValue.size.height;
    if (!self.bgView.hidden) {
        self.tagView.bottom = SCREEN_HEIGHT - (keyboardH + 15 + 40 + Tabbar_Bottom_height);
        self.bgImageView.top = self.tagView.bottom - 10;
    }
}

/// 外部获取选中的参数
- (NSDictionary *)getParamsDic
{
    self.searchModel.inlayPortMin = self.xkScopeView.minTF.text;
    self.searchModel.inlayPortMax = self.xkScopeView.maxTF.text;
    self.searchModel.handInchMin = self.scScopeView.minTF.text;
    self.searchModel.handInchMax = self.scScopeView.maxTF.text;
//    self.searchModel.goldWeightMin = self.jzScopeView.minTF.text;
//    self.searchModel.goldWeightMax = self.jzScopeView.maxTF.text;
    self.searchModel.braceletSizeMin = self.szScopeView.minTF.text;
    self.searchModel.braceletSizeMax = self.szScopeView.maxTF.text;
    self.searchModel.sourceType = @3;
    
    NSDictionary *dic = [self.searchModel modelToDic];
    
    return dic;
}

- (void)setupViewsWithName:(NSString *)name
{
    if ([@[@"女戒", @"对戒", @"男戒"] containsObject:name]) {
        self.scScopeView.hidden = NO;
        self.szScopeView.hidden = YES;
    } else {
        if ([@"手镯" isEqualToString:name]) {
            self.scScopeView.hidden = YES;
            self.szScopeView.hidden = NO;
        } else {
            self.scScopeView.hidden = YES;
            self.szScopeView.hidden = YES;
        }
    }
    
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat czTop = self.colorView.hidden ? self.colorView.top : self.cutView.bottom + 10;
    self.czView.top = czTop;
    self.xfView.top = self.czView.bottom + 10;
    self.xfSubView.top = self.xfView.bottom + 10;
    self.jbView.top = self.xfSubView.hidden ? self.xfView.bottom + 10 : self.xfSubView.bottom + 10;
    self.fsView.top = self.jbView.bottom + 10;
    self.xkScopeView.top = self.fsView.bottom + 10;
    self.scScopeView.top = self.xkScopeView.bottom + 10;
    self.szScopeView.top = self.xkScopeView.bottom + 10;
//    self.jzScopeView.top = (self.scScopeView.hidden && self.szScopeView.hidden) ? self.xkScopeView.bottom + 10 : self.scScopeView.bottom + 10;
    
    self.scrollView.contentSize = CGSizeMake(self.width, _szScopeView.bottom + 30);
}

- (void)setupSubviews
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self addSubview:self.scrollView];
    
    CGFloat itemW = (self.width - 33) / 3;
    LLTagConfig *config = [[LLTagConfig alloc] init];
    config.minItemSpace = 8;
    config.minLineSpace = 5;
    config.sectionInset = UIEdgeInsetsMake(0, 6, 0, 4);
    config.itemHeight = 30;
    config.itemMinWidth = itemW;
    config.tagNormalbackgroundColor = UIColorHex(#F5F5F5);
    config.tagSelectbackgroundColor = UIColorHex(#F5F0FF);
    config.titleNormalbackgroundColor = UIColorHex(#333333);
    config.titleSelectbackgroundColor = UIColorHex(#6822B7);
    config.cornerRadius = 15;
    config.normalBoardColor = UIColorHex(#F5F5F5);
    config.selectBoardColor = UIColorHex(#8B57E6);
    
    __block BOOL isCpResetView = NO;
    @weakify(self)
    _cpView = [[TTZuanshiScopeView alloc] initWithFrame:CGRectMake(0, 50, self.width, 0) title:@"产品属性" datasArray:@[@"空托", @"成品"] selectBlock:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        NSNumber *finishedProduct = @0;
        if ([selectArray.firstObject isEqualToString:@"空托"]) {
            finishedProduct = @1;
        } else if ([selectArray.firstObject isEqualToString:@"成品"]) {
            finishedProduct = @2;
        }
        BOOL show = finishedProduct.intValue == 2;
        self.colorView.hidden = !show;
        self.jdView.hidden = !show;
        self.cutView.hidden = !show;
        if (!show) {
            isCpResetView = YES;
            [self.colorView resetView];
            [self.jdView resetView];
            [self.cutView resetView];
            isCpResetView = NO;
        }
        
        self.searchModel.finishedProduct = finishedProduct;
        [self setSearchNameDicWithKey:@"1" value:[selectArray componentsJoinedByString:@" "]];
        
        // [self searchParamsChanged];
        
        [self setNeedsLayout];
        
    } config:config];
    _cpView.canCancelSelect = YES;
    [self.scrollView addSubview:_cpView];
    
    _colorView = [[TTZuanshiScopeView alloc] initWithFrame:CGRectMake(0, _cpView.bottom + 10, self.width, 0) title:@"成品主石颜色" datasArray:@[@"D-E", @"F-G", @"H", @"I-J", @"K-l", @"M-N", @"<N", @"黑色", @"白色"] selectBlock:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        self.searchModel.sStoneColor = [selectArray componentsJoinedByString:@","];
        [self setSearchNameDicWithKey:@"2" value:self.searchModel.sStoneColor];
        if (!isCpResetView) {
            // [self searchParamsChanged];
        }
    } config:config];
    _colorView.hidden = YES;
    _colorView.canCancelSelect = YES;
    [self.scrollView addSubview:_colorView];
    
    _jdView = [[TTZuanshiScopeView alloc] initWithFrame:CGRectMake(0, _colorView.bottom + 10, self.width, 0) title:@"成品主石净度" datasArray:@[@"LC", @"VVS", @"VS", @"SI", @"P"] selectBlock:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        self.searchModel.sStoneClarity = [selectArray componentsJoinedByString:@","];
        [self setSearchNameDicWithKey:@"3" value:self.searchModel.sStoneClarity];
        if (!isCpResetView) {
            // [self searchParamsChanged];
        }
    } config:config];
    _jdView.hidden = YES;
    _jdView.canCancelSelect = YES;
    [self.scrollView addSubview:_jdView];
    
    _cutView = [[TTZuanshiScopeView alloc] initWithFrame:CGRectMake(0, _jdView.bottom + 10, self.width, 0) title:@"成品主石切工" datasArray:@[@"十二心十二箭", @"十心十箭", @"八心八箭", @"很好"] selectBlock:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        self.searchModel.sStoneCut = [selectArray componentsJoinedByString:@","];
        [self setSearchNameDicWithKey:@"4" value:self.searchModel.sStoneCut];
        if (!isCpResetView) {
            // [self searchParamsChanged];
        }
    } config:config];
    _cutView.hidden = YES;
    _cutView.canCancelSelect = YES;
    [self.scrollView addSubview:_cutView];
    
    
    _czView = [[TTZuanshiScopeView alloc] initWithFrame:CGRectMake(0, _cpView.bottom + 10, self.width, 0) title:@"材质" datasArray:@[@"18K", @"PT"] selectBlock:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        if ([selectArray.firstObject isEqualToString:@"18K"]) {
            self.searchModel.material = @"AU750";
        } else if ([selectArray.firstObject isEqualToString:@"PT"]){
            self.searchModel.material = @"PT950";
        } else {
            self.searchModel.material = nil;
        }
//        self.searchModel.material = selectArray.firstObject;
        [self setSearchNameDicWithKey:@"9" value:selectArray.firstObject];
        // [self searchParamsChanged];
    } config:config];
    _czView.canCancelSelect = YES;
    [self.scrollView addSubview:_czView];
    
    __block BOOL xfSubViewIsReset = NO;
    NSDictionary *dic = @{
                          @"爪镶" : @[@"3-4爪", @"5-6爪", @"爪镶其他"],
                          @"包夹镶" : @[@"2-3叶包", @"4-6叶包", @"包夹其他"],
                          @"群镶" : @[]
                          };
    
    _xfView = [[TTZuanshiScopeView alloc] initWithFrame:CGRectMake(0, _czView.bottom + 10, self.width, 0) title:@"镶法" datasArray:@[@"爪镶", @"包夹镶", @"群镶"] selectBlock:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        NSLog(@">>> %@", selectArray.firstObject);
        xfSubViewIsReset = YES;
        self.xfSubView.dataArray = dic[selectArray.firstObject];
        [self.xfSubView resetView];
        self.xfSubView.hidden = [dic[selectArray.firstObject] count] == 0;
        
        [self setNeedsDisplay];
        
        self.searchModel.childProcess = nil;
        [self.searchNameDic removeObjectForKey:@"11"];
        [self setNeedsLayout];
        self.searchModel.process = selectArray.firstObject;
        [self setSearchNameDicWithKey:@"10" value:selectArray.firstObject];
        
        // [self searchParamsChanged];
    } config:config];
    _xfView.canCancelSelect = YES;
    [self.scrollView addSubview:_xfView];
    
    
    _xfSubView = [[TTZuanshiScopeView alloc] initWithFrame:CGRectMake(0, _xfView.bottom + 0, self.width, 0) title:@"" datasArray:@[] selectBlock:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        self.searchModel.childProcess = selectArray.firstObject;
        [self setSearchNameDicWithKey:@"11" value:selectArray.firstObject];
        if (!xfSubViewIsReset) {
            // [self searchParamsChanged];
        } else {
            xfSubViewIsReset = NO;
        }
    } config:config];
    _xfSubView.hidden = YES;
    _xfSubView.canCancelSelect = YES;
    [self.scrollView addSubview:_xfSubView];
    
    
    _jbView = [[TTZuanshiScopeView alloc] initWithFrame:CGRectMake(0, _xfView.bottom + 10, self.width, 0) title:@"戒臂" datasArray:@[@"直臂", @"扭臂"] selectBlock:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        self.searchModel.ringArms = selectArray.firstObject;
        [self setSearchNameDicWithKey:@"12" value:selectArray.firstObject];
        // [self searchParamsChanged];
    } config:config];
    _jbView.canCancelSelect = YES;
    [self.scrollView addSubview:_jbView];
    
    
    _fsView = [[TTZuanshiScopeView alloc] initWithFrame:CGRectMake(0, _jbView.bottom + 10, self.width, 0) title:@"副石" datasArray:@[@"有", @"无"] selectBlock:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        NSString *has = [selectArray.firstObject isEqualToString:@"有"] ? @"true" : @"false";
        self.searchModel.isExistVStone = has;
        NSString *str = [selectArray.firstObject isEqualToString:@"有"] ? @"副石" : @"";
        [self setSearchNameDicWithKey:@"13" value:str];
        // [self searchParamsChanged];
    } config:config];
    _fsView.canCancelSelect = YES;
    [self.scrollView addSubview:_fsView];
    
    
    _xkScopeView = [[TTZuanshiTextScopeView alloc] initWithFrame:CGRectMake(0, _fsView.bottom + 10, self.width, 0) title:@"镶口" startEdit:^(BOOL isLeftTF) {
        @strongify(self)
        [self setBgImageViewLeftPoint:isLeftTF];
        self.selectScopeView = self.xkScopeView;
        [self setupScopeTextAlertViewWithArray:self.xkArray];
    }];
    [self.scrollView addSubview:_xkScopeView];
    
    _scScopeView = [[TTZuanshiTextScopeView alloc] initWithFrame:CGRectMake(0, _xkScopeView.bottom + 10, self.width, 0) title:@"手寸" startEdit:^(BOOL isLeftTF) {
        @strongify(self)
        [self setBgImageViewLeftPoint:isLeftTF];
        self.selectScopeView = self.scScopeView;
        [self setupScopeTextAlertViewWithArray:self.scArray];
    }];
    [self.scrollView addSubview:_scScopeView];
    
    _szScopeView = [[TTZuanshiTextScopeView alloc] initWithFrame:CGRectMake(0, _xkScopeView.bottom + 10, self.width, 0) title:@"手镯内径" startEdit:^(BOOL isLeftTF) {
        @strongify(self)
        [self setBgImageViewLeftPoint:isLeftTF];
        self.selectScopeView = self.szScopeView;
        [self setupScopeTextAlertViewWithArray:self.szArray];
    }];
    _szScopeView.hidden = YES;
    [self.scrollView addSubview:_szScopeView];
    
//    _jzScopeView = [[TTZuanshiTextScopeView alloc] initWithFrame:CGRectMake(0, _scScopeView.bottom + 10, self.width, 0) title:@"金重" startEdit:^(BOOL isLeftTF) {
//        @strongify(self)
//        [self setBgImageViewLeftPoint:isLeftTF];
//        self.selectScopeView = self.jzScopeView;
//        [self setupScopeTextAlertViewWithArray:self.jzArray];
//    }];
//    [self.scrollView addSubview:_jzScopeView];
    
    self.scrollView.contentSize = CGSizeMake(self.width, _szScopeView.bottom + 30);
    
    
    
    [self.allViewsArray addObject:_cpView];
    [self.allViewsArray addObject:_colorView];
    [self.allViewsArray addObject:_jdView];
    [self.allViewsArray addObject:_cutView];
    [self.allViewsArray addObject:_czView];
    [self.allViewsArray addObject:_xfView];
    [self.allViewsArray addObject:_xfSubView];
    [self.allViewsArray addObject:_jbView];
    [self.allViewsArray addObject:_fsView];
    [self.allViewsArray addObject:_xkScopeView];
    [self.allViewsArray addObject:_scScopeView];
    [self.allViewsArray addObject:_szScopeView];
//    [self.allViewsArray addObject:_jzScopeView];
    
}

- (void)setBgImageViewLeftPoint:(BOOL)isLeftTF
{
    self.isLeftTF = isLeftTF;
    CGFloat left = self.left == 0 ? kScreenWidth *1/6 : self.left;
    [UIView animateWithDuration:0.2 animations:^{
        self.bgImageView.left = left + self.width * (isLeftTF ? 0.2 : 0.71);
    }];
}

- (void)resetViews
{
    for (UIView *view in self.allViewsArray) {
        if ([view respondsToSelector:@selector(resetView)]) {
            [view performSelector:@selector(resetView)];
        }
    }
    // 先取出之前的值
    NSString *categoryName = self.searchModel.categoryName;
    [self.searchNameDic removeAllObjects];
    [self setSearchNameDicWithKey:@"0" value:categoryName];
    [self setSearchNameDicWithKey:@"11" value:nil];
    self.colorView.hidden = YES;
    
    self.searchModel = nil;
    self.searchModel = [[TTSearchModel alloc] init];
    self.searchModel.sourceType = @(1);
    self.searchModel.categoryName = categoryName;
    
    [self setNeedsLayout];
}

//- (void)searchParamsChanged
//{
//    /// 参数发生变化发送通知
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"zuanshi_chooseItemsChangedNotify_key" object:[self getParamsDic]];
//}

// 设置搜索名称
- (void)setSearchNameDicWithKey:(NSString *)key value:(NSString *)value
{
    self.flag = @"1";
    
    if (value == nil || key == nil) {
        [self.searchNameDic removeObjectForKey:key];
        return;
    }
    NSString *str = value.length > 0 ? value : @"";
    [self.searchNameDic setObject:str forKey:key];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    NSString *newF = change[NSKeyValueChangeNewKey];
    NSString *oldF = change[NSKeyValueChangeOldKey];
    
    if ([newF isEqualToString:oldF]) {
        return;
    }
    if (newF.intValue != 1) {
        return;
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        /// 参数发生变化发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"zuanshi_chooseItemsChangedNotify_key" object:[self getParamsDic]];
        
        self.flag = @"0";
    });
}


- (void)setXkDataArray:(NSArray *)xkArray scDataArray:(NSArray *)scArray jzDataArray:(NSArray *)jzArray szDataArray:(NSArray *)szArray
{
    self.xkArray = xkArray;
    self.scArray = scArray;
    self.jzArray = jzArray;
    self.szArray = szArray;
    
}


- (void)setupScopeTextAlertViewWithArray:(NSArray *)array
{
    self.selectArray = array;
    
    self.bgView.hidden = NO;
    
    NSMutableArray *titleArray = [NSMutableArray array];
    for (TTSearchInsertModel *model in array) {
        [titleArray addObject:model.title];
    }
    
    [self createTextScopeChooseView];
    
    [self.tagView setupDatasArray:titleArray];
    
}

- (void)createTextScopeChooseView
{
    if (_bgView) {
        [_bgView removeFromSuperview];
        [[UIApplication sharedApplication].keyWindow addSubview:_bgView];
        return;
    }
    _bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [[UIApplication sharedApplication].keyWindow addSubview:_bgView];
    
    CGFloat itemW = (self.width - 60) / 3;
    LLTagConfig *config = [[LLTagConfig alloc] init];
    config.minItemSpace = 8;
    config.minLineSpace = 8;
    config.sectionInset = UIEdgeInsetsMake(10, 8, 10, 8);
    config.itemHeight = 27;
    config.itemMinWidth = itemW;
    config.tagNormalbackgroundColor = UIColor.whiteColor;
    config.tagSelectbackgroundColor = UIColorHex(#F5F0FF);
    config.titleNormalbackgroundColor = UIColorHex(#333333);
    config.titleSelectbackgroundColor = UIColorHex(#6822B7);
    config.cornerRadius = 15;
    config.fontSize = 12;
    config.normalBoardColor = UIColorHex(#F5F5F5);
    config.selectBoardColor = UIColorHex(#8B57E6);
    
    @weakify(self)
    CGFloat left = self.left == 0 ? kScreenWidth *1/6 : self.left;
    self.tagView = [[LLTagView alloc] initWithFrame:CGRectMake(left + 10, 5, self.width - 20, 0) dataArray:@[] selectItems:^(NSArray * _Nonnull selectArray) {
        @strongify(self)
        self.bgView.hidden = YES;
        for (int i = 0; i < self.selectArray.count; i++) {
            TTSearchInsertModel *model = self.selectArray[i];
            if ([selectArray.firstObject isEqualToString:model.title]) {
                [self.selectScopeView setMinStr:model.min maxStr:model.max];
                break;
            }
        }
    } config:config];
    self.tagView.backgroundColor = UIColorHex(#E6E6E6);
    self.tagView.layer.cornerRadius = 10;
    self.tagView.layer.masksToBounds = YES;
    [self.bgView addSubview:self.tagView];
    
    left = left + self.width * (_isLeftTF ? 0.2 : 0.71);
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(left, self.tagView.bottom, 25, 25)];
    bgImageView.image = [UIImage imageNamed:@"三角下标"];
    [self.bgView addSubview:bgImageView];
    self.bgImageView = bgImageView;
    
}

/// 设置选中状态的items
- (void)setSelectItemsWithParamDic:(NSDictionary *)paramsDic
{
    NSString *finishedProductKey = @"param.finishedProduct";
    NSString *colorKey = @"param.sStoneColor";
    NSString *jdKey = @"param.sStoneClarity";
    NSString *cutKey = @"param.sStoneCut";
    NSString *czKey = @"param.material";
    NSString *xfKey = @"param.process";
    NSString *xfSubKey = @"param.childProcess";
    NSString *jbKey = @"param.ringArms";
    NSString *fsKey = @"param.isExistVStone";
    NSString *xkMin = @"param.inlayPortMin";
    NSString *xkMax = @"param.inlayPortMax";
    NSString *scMin = @"param.handInchMin";
    NSString *scMax = @"param.handInchMax";
    NSString *szMin = @"param.braceletSizeMin";
    NSString *szMax = @"param.braceletSizeMax";
//    NSString *jzMin = @"param.goldWeightMin";
//    NSString *jzMax = @"param.goldWeightMax";
    
    self.searchModel.categoryName = paramsDic[@"param.categoryName"];
    
    NSArray *keys = paramsDic.allKeys;
    // 产品
    if ([keys containsObject:finishedProductKey]) {
        if ([paramsDic[finishedProductKey] intValue] == 1) {
            [self.cpView setSelectIndex:0];
        } else if ([paramsDic[finishedProductKey] intValue] == 2) {
            [self.cpView setSelectIndex:1];
        }
    }
    // 颜色
    NSMutableArray *colorsIndexs = [self getDefaultSelectedIndexsWithParamsDic:paramsDic key:colorKey array:self.colorView.dataArray];
    if (colorsIndexs != nil) {
        [self.colorView setSelectIndexs:colorsIndexs];
    }

    // 净度
    NSMutableArray *jdIndexs = [self getDefaultSelectedIndexsWithParamsDic:paramsDic key:jdKey array:self.jdView.dataArray];
    if (jdIndexs != nil) {
        [self.jdView setSelectIndexs:jdIndexs];
    }

    // 切工
    NSMutableArray *cutIndexs = [self getDefaultSelectedIndexsWithParamsDic:paramsDic key:cutKey array:self.cutView.dataArray];
    if (cutIndexs != nil) {
        [self.cutView setSelectIndexs:cutIndexs];
    }
    
    // 材质
    if ([keys containsObject:czKey]) {
        NSInteger index = 0;
        if ([paramsDic[czKey] isEqualToString:@"AU750"]) {
            index = 0;
        } else {
            index = 1;
        }
//        NSInteger index = [self.czView.dataArray indexOfObject:paramsDic[czKey]];
        [self.czView setSelectIndex:index];
    }
    // 镶法
    if ([keys containsObject:xfKey]) {
        NSInteger index = [self.xfView.dataArray indexOfObject:paramsDic[xfKey]];
        [self.xfView setSelectIndex:index];
    }
    // 镶法子选项
    if ([keys containsObject:xfSubKey]) {
        NSInteger index = [self.xfSubView.dataArray indexOfObject:paramsDic[xfSubKey]];
        [self.xfSubView setSelectIndex:index];
    }
    // 戒臂
    if ([keys containsObject:jbKey]) {
        NSInteger index = [self.jbView.dataArray indexOfObject:paramsDic[jbKey]];
        [self.jbView setSelectIndex:index];
    }
    // 副石
    if ([keys containsObject:fsKey]) {
        NSInteger index = [@"true" isEqualToString:paramsDic[fsKey]] ? 0 : 1;
        [self.fsView setSelectIndex:index];
    }
    
    [self.xkScopeView setMinStr:paramsDic[xkMin] maxStr:paramsDic[xkMax]];
    [self.scScopeView setMinStr:paramsDic[scMin] maxStr:paramsDic[scMax]];
    [self.szScopeView setMinStr:paramsDic[szMin] maxStr:paramsDic[szMax]];
//    [self.jzScopeView setMinStr:paramsDic[jzMin] maxStr:paramsDic[jzMax]];
    
    self.flag = @"1";
    
}

- (NSMutableArray *)getDefaultSelectedIndexsWithParamsDic:(NSDictionary *)paramsDic key:(NSString *)key array:(NSArray *)array
{
    NSArray *keys = paramsDic.allKeys;
    NSMutableArray *indexs = [NSMutableArray array];
    if ([keys containsObject:key]) {
        NSString *value = paramsDic[key];
        NSArray *valueArray = [value componentsSeparatedByString:@","];
        for (int i = 0; i < valueArray.count; i++) {
            for (int j = 0; j < array.count; j++) {
                if ([array[j] isEqualToString:valueArray[i]]) {
                    [indexs addObject:@(j)];
                    break;
                }
            }
        }
        return indexs;
    }
    return nil;
}




- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self removeObserver:self forKeyPath:@"flag"];
}

@end
